/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 8/6/2008
 * Time: 6:16 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Collections;
using System.Text;

namespace popLib
{
	/// <summary>
	/// Description of fileCls.
	/// </summary>
	public class FileLib
	{
		static int counter = 0;
		public static string readFile(string file){
			if(File.Exists(file)){
				StreamReader sr = new StreamReader(file);
				string content = sr.ReadToEnd();
				sr.Close();
				return content;
			}
			return "";
		}
		
		
		public static void writeFile(string file, string content){
            writeFile(file, content, false, Encoding.Unicode);
		}

        public static void writeFile(string file, string content, bool append)
        {
            writeFile(file, content, append, Encoding.Unicode);
        }
               		
		public static void writeFile(string file, string content, bool append, Encoding encoding){
        	try{
	        	StreamWriter sw = new StreamWriter(file, append, encoding);
				sw.Write(content);
				sw.Flush();
				sw.Close();
        	}catch(Exception ex){FormLib.alert(ex);}
            
		}
		
		public enum SearchOption{ FileOnly, FolderOnly, Both }
		public static string[] getFileList(string path, string filter, bool includeSubfolder, SearchOption searchOption){			
			string[] files 	= new string[0];
			string[] folers = new string[0];
			
			if(!Directory.Exists(path)) return files;
			
			ArrayList temp 	= new ArrayList();
			if( filter == null ) filter = "*";
			
			if( searchOption.Equals(SearchOption.Both) || searchOption.Equals(SearchOption.FileOnly) ){
				files  = Directory.GetFiles( path, filter );
				temp.AddRange(files);
			}
			if( searchOption.Equals(SearchOption.Both) || searchOption.Equals(SearchOption.FolderOnly) ){
				folers = Directory.GetDirectories( path, filter );
				temp.AddRange(folers);
			}
		
			if(includeSubfolder){
				string[] directories = Directory.GetDirectories( path );
				foreach( string directory in directories )
					temp.AddRange(getFileList( directory, filter, includeSubfolder, searchOption ));				
			}
			
			files = new string[temp.Count];
			temp.CopyTo( files, 0 );
			return files;
		}
				
		public static void copyStream(Stream source, Stream destination)
		{
			byte[] buffer = new byte[4096];
			int countBytesRead;
			while ((countBytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
			{
				destination.Write(buffer, 0, countBytesRead);
			}
		}
		
		public static string getRandomName(){
			return DateTime.Now.ToString("yyyyMMdd-HHmmss") + "("+(counter++)+")";
		}
		
		public static string getRandomName(string prefix){
			return prefix + DateTime.Now.ToString("yyyyMMdd-HHmmss") + "("+(counter++)+")";
		}
		
		public static string getRandomName(string prefix, string extension){
			return prefix + DateTime.Now.ToString("yyyyMMdd-HHmmss") + "("+(counter++)+")." + extension;
		}
		
		public static string validateFilename(string filename){
			char[] invalidChar = Path.GetInvalidFileNameChars();
			for( int i=0; i<invalidChar.Length;i++ ){
				filename = filename.Replace( invalidChar[i].ToString() ,"");
			}
			return filename;
		}
		
		public enum WriteLogType{ DateTime_Log, DateTime_Explain_Log }		
		public static void WriteLog( string file, object exception ){
			string content = String.Format( "[{0}]: {1}\r\n\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), exception );
			FileLib.writeFile( file, content, true );
		}
		
		public static void WriteLog( string file, object exception, string explain ){
			string content = String.Format( "[{0}]: {1}\r\n{2}\r\n\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), explain, exception );
			FileLib.writeFile( file, content, true );
		}
		
		public static void WriteAppLog( object exception ){
			WriteAppLog( exception, null );
		}
		
		public static void WriteAppLog( object exception, string explain ){
			string file = FileLib.getFullPath( Application.ProductName + "_" + DateTime.Now.ToString("yyyy-MM-dd") +".log" );
			WriteLog( file, exception, explain );
		}
		
		public static bool isValid( string file, bool isFile ){
			if( !StringLib.isValid(file) ) return false;
			if( isFile && !File.Exists(file) ) return false;
			if( !isFile && !Directory.Exists(file) ) return false;
			return true;
		}
		
		public static string getFullPath(string path){
			return Path.Combine( Application.StartupPath, path );
		}
	}
}
